AddCSLuaFile( 'cl_init.lua' )
AddCSLuaFile( 'shared.lua' )
include( 'shared.lua' )

function  ENT:Initialize()
	self.BaseClass.Initialize(self)

	self.Entity:DrawShadow( false )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )

	if (self:IsTree()) or (self:GetModel() == "models/props_foliage/tree_stump01.mdl") then
		self.Entity:SetMoveType( MOVETYPE_NONE )
	else
		self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	end

	self.Entity:SetSolid( SOLID_VPHYSICS )

	self.Life = 100

	local phys = self:GetPhysicsObject()

	if (phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:PhysicsSetup()
	local mins = self.Entity:OBBMins() * self.FireReduce
	local maxs = self.Entity:OBBMaxs() * self.FireReduce

	self.Entity:PhysicsInitBox( mins, maxs )
	self.Entity:SetCollisionBounds( mins, maxs )
end

function ENT:OnRemove()
	if (!self.Owner) or (!self.Owner:IsValid()) or (!self.Type) then return end
	self.Owner:RemoveItemCount( self.Type )
end


function ENT:Think()
	self.BaseClass.Think(self)

	--[[
		Fire Spreading
	]]
	self:FireSpread()

	--[[
		Tree Reproduce
	]]
	self:TreeReproduce()


	self.Entity:NextThink( CurTime() + 1 )
	return true
end

function ENT:OnTakeDamage( dmg )
	self.BaseClass.OnTakeDamage( self, dmg )

	if (self:IsTree()) and (dmg:GetInflictor():IsWeapon()) then
		self:TreeHatchet( dmg )
	end

	if (!self:GetNWBool( "Dead" )) then self:SetNWBool( "Dead", true ) end
end

function ENT:Use( activator, caller )
	if ( !activator:IsPlayer() ) then return end

	if (!self:IsLog()) then return end

	local wood = math.random(1, 6) * (activator:GetSkill("Lumbering") + 1)

	activator:AddInventory( "Wood", wood )
	activator:EmitSound( Sound("items/ammo_pickup.wav") )

	self.Entity:Remove()
end











function ENT:FireSpread()
	if (!self:IsOnFire()) then return end


	self.MaxHeight = (self.MaxHeight or 1) - 0.005

	if (self.MaxHeight <= 0) then
		self:Remove()
		return
	end

	self:SetNWFloat("Resize", self.MaxHeight)

	--[[
		cant call PhysicsSetup to often (crashes game) so using a time check

	if (CurTime() >= (self.NextPrintTime or 0)) then
		self:PhysicsSetup()
		self.NextPrintTime = CurTime() + 1.5
	end
]]

	--[[
		Get near by props that we can light up :)

	if math.random(1, 5) == 2 then
		for _, ent in pairs( ents.FindInSphere( self.Entity:GetPos(), 10 ) or {} ) do
			if (ent != self.Entity)
				and (!ent:IsOnFire())
				and (ent:IsLog() or ent:IsTree())
			then
				if math.random(1, 2) == 1 then
					ent:Ignite()
				end
			end
		end
	end]]
end

function ENT:TreeReproduce()
	if (!self:IsTree()) then return end

	if (math.random(1,30) == 1) and (self:IsTree()) and (self:GetNWBool( "Dead" ) != true) then
		local pos = self:GetPos() + Vector(math.random(-500,500),math.random(-500,500),0)

		--create a tree but only if no other props are within distance
		if (table.Count(ents.FindInSphere( pos, 300 )) == 0) then CreateEntity( nil, pos, "Tree", true ) end
	end
end

function ENT:TreeHatchet( dmg )
	local damage = dmg:GetDamage()
	local ply = dmg:GetAttacker()

	--old way of doing things, now people have to pick up the logs :)
	--ply:AddInventory( "damage", damage )
	--ply:EmitSound( Sound("items/ammo_pickup.wav") )

	--one in 20 chance for the players skill to increase
	if (math.random(1, 20) == 20) then
		ply:AddSkill( "Lumbering", 1 )	--increase lumber skill by 1
		ply:EasyMessage( "Lumber Skill Increased to " .. ply:GetSkill( "Lumbering" ), 5 )
	end

	--reduce life
	self.Life = math.Clamp(self.Life - damage, 0, 20)

	--if tree is now dead break it up into logs
	if (self.Life == 0) then
		local pos = self:GetPos()
		local height = (self:OBBMins() - self:OBBMaxs()):Length()
		local onfire = self:IsOnFire()

		--remove tree since its chopped up now
		self:Remove()

		--Create a new stump entity to replace tree
		local stump = createStump( pos )

		--get the first position to place the logs
		local pos = stump:GetPos() + Vector(0,0,height)

		--max amount of logs to create per tree
		local maxLogs = 2 * (ply:GetSkill( "Lumbering" ) + 1)
		local inc = 1

		--spawn logs for the length/height of the tree
		while (height > 0) do
			--stop loop if we have reached the max amount of logs
			if (inc >= maxLogs) then break end
			
			--create log
			local log = createLog( pos, onfire )

			--get the size of the log
			local size = (log:OBBMins() - log:OBBMaxs()):Length()

			--update pos and height
			pos = pos - Vector(0, 0, size)	--reduce vector for next log spawn position
			height = height - size	--reduce height to spawn next log
			inc = inc + 1 --increase log spawned count
		end
	end
end